<?php

//decode by http://www.yunlu99.com/
require MODULE_ROOT . '/helpers/functions.php';
global $_GPC, $_W;
$op = $_GPC['op'] ?: 'index';
$user = user();
$config = config();
$uniacid = uniacid();
$uid = uid();
check_auth();
if ($op == 'index') {
	$where = ["uniacid = {$uniacid}"];
	$groups = pdo_fetchall('select * from ' . tablename('d1sj_card_group') . " where uniacid=:uniacid order by orderby desc limit 3", array(':uniacid' => $uniacid));
	$user_list = pdo_fetch('select * from ' . tablename('d1sj_card_user') . " where uniacid=:uniacid and uid=:uid ", array(':uniacid' => $_W['uniacid'], ':uid' => uid()));
	include $this->template('vip');
} else {
	if ($op == 'submit') {
		$code = $_GPC['code'];
		$group = $_GPC['group'];
		$yuan_group = $_GPC['yuan_group'];
		$user_id = $_GPC['user_id'];
		$name = $_GPC['name'];
		$mobile = $_GPC['mobile'];
		$users = pdo_fetch('select * from ' . tablename('d1sj_card_user') . " where uniacid=:uniacid and mobile=:mobile and name=:name", array(':uniacid' => $uniacid, ':mobile' => $mobile, ':name' => $name));
		if (!$users) {
			message('用户信息请跟个人资料保持一致');
		}
		if ($code != $_COOKIE['code']) {
			message('验证码错误！');
		}
		if ($users['group'] == $group) {
			message('您已经是该类会员，请升级');
		}
		$userGroup = get_group_info_by_id($users['group']);
		$info = get_group_info_by_id($group);
		$yuan_info = get_group_info_by_id($yuan_group);
		if ($userGroup && $userGroup['mlevel'] > $info['mlevel']) {
			message('您是高级会员，不能降级！');
		}
		$res = $this->member_muban($_W['openid'], $config['member_template_id'], $yuan_info['name'], $info['name']);
		$user_lisrt = pdo_fetch('select * from ' . tablename('d1sj_card_user') . " where uid=:uid and uniacid=:uniacid", array(':uid' => uid(), ':uniacid' => $_W['uniacid']));
		if ($user_lisrt['leader1']) {
			$user_to_lisrt = pdo_fetch('select * from ' . tablename('d1sj_card_user') . " where uid=:uid and uniacid=:uniacid", array(':uid' => $user_lisrt['leader1'], ':uniacid' => $_W['uniacid']));
			$res = $this->member_to_muban($user_to_lisrt['openid'], $config['member_template_id'], $yuan_info['name'], $info['name']);
		}
		$orderNo = get_order_sn();
		$data = ['uniacid' => $uniacid, 'group' => $group, 'price' => $info['price'], 'uid' => uid(), 'status' => 2, 'create_time' => time(), 'sn' => $orderNo];
		pdo_insert('d1sj_card_order', $data);
		$params = ['tid' => $orderNo, 'ordersn' => $orderNo, 'title' => '开通会员', 'fee' => $info['price'], 'user' => $uid];
		$this->pay($params);
	} else {
		if ($op == 'VerificationCode') {
			if ($_W['isajax']) {
				$mobile = $_GPC['mobile'];
				$smsapi = "http://www.smsbao.com/";
				$user = $config['info_name'];
				$pass = md5($config['info_password']);
				$rand_code = rand_string();
				setcookie("code", $rand_code, time() + 7200);
				$content = "您的验证码是" . $rand_code;
				$phone = $mobile;
				$sendurl = $smsapi . "sms?u=" . $user . "&p=" . $pass . "&m=" . $phone . "&c=" . urlencode($content);
				$result = file_get_contents($sendurl);
				if ($result == 0) {
					die(json_encode(array('info' => 1, 'msg' => '发送成功')));
				} else {
					die(json_encode(array('info' => 2, 'msg' => '发送失败请联系管理员')));
				}
			}
		} else {
			if ($op == 'contract') {
				$contract = $_GPC['contract'];
				$conf = pdo_fetch('select contract_noe,contract_tow from ' . tablename('d1sj_card_setting') . " where uniacid=:uniacid", array(':uniacid' => $_W['uniacid']));
				include $this->template('vip');
			}
		}
	}
}
function rand_string($len = 6)
{
	$chars = str_repeat('0123456789', $len);
	$chars = str_shuffle($chars);
	$str = substr($chars, 0, $len);
	return $str;
}